import './assets/main.css'
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import pinia from './store'
import axios from 'axios'
import '@fortawesome/fontawesome-free/css/all.css'

// 配置axios默认baseURL
axios.defaults.baseURL = 'http://localhost:8080'

// 从localStorage恢复token到axios请求头
try {
  const token = localStorage.getItem('token')
  if (token) {
    axios.defaults.headers.common['Authorization'] = `Bearer ${token}`
  }
} catch (error) {
  console.error('Failed to restore token from localStorage', error)
  // 清除可能损坏的数据
  localStorage.removeItem('token')
}

// 初始化主题
const initializeTheme = () => {
  // 优先使用localStorage存储的主题
  const savedTheme = localStorage.getItem('theme-mode');
  
  if (savedTheme) {
    document.documentElement.setAttribute('data-theme', savedTheme);
    return;
  }
  
  // 如果没有存储的主题，则使用系统主题
  const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  const defaultTheme = prefersDark ? 'dark' : 'light';
  
  localStorage.setItem('theme-mode', defaultTheme);
  document.documentElement.setAttribute('data-theme', defaultTheme);
};

// 执行主题初始化
initializeTheme();

// 创建Vue应用
const app = createApp(App)

// 使用插件
app.use(router)
app.use(pinia)

// 挂载应用
app.mount('#app')
